package com.it.core.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.it.core.po.User;
import com.it.core.po.Word;
import com.it.core.service.WordService;

//留言控制类
@Controller
public class WordController {

	//依赖注入
	@Autowired
	public WordService wordService;
	
	// 返回留言列表
	@RequestMapping(value= "/list.action")
	public String list(Model model) {
		List<Word> list =(List<Word>)wordService.selectWordList();
		for(Word word : list) {
			System.out.println(word);
		}
		
		model.addAttribute("words", list);
		return "main";
	}
	
	// 添加留言
	@RequestMapping(value= "/addWord.action")
	public String addWord(Word word ,HttpSession session) {
		// 获取session中当前的用户信息
		User user = (User) session.getAttribute("USER_SESSION");
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(date);
		word.setWtime(dateString);
		word.setUid(user.getUid());
		word.setUsername(user.getUsername());
		word.setWpraise(0);
		wordService.addWord(word);
		return "redirect:list.action";
		
	}
	
	// 添加点赞
	@RequestMapping(value= "/addZan")
	@ResponseBody
	public String addZan(Integer wid) {
		wordService.addZan(wid);
		return "OK";
	}
	
	
}
